<template>
  <!-- // 分配角色 -->
  <div>
    <el-checkbox-group v-model="roleIds">
      <el-checkbox v-for="item in list" :key="item.id" label="item.id">{{ item.name }}</el-checkbox>
    </el-checkbox-group>

    <div style="margin-top: 20px; text-align: right">
      <el-button type="primary" @click="hSubmit">确定</el-button>
      <el-button @click="closeDialog">取消</el-button>
    </div>
  </div>
</template>
<script>
import { getRoles } from '@/api/settings'
import { getUserProfileId } from '@/api/user'
import { assignRoles } from '@/api/employees'
export default {
  props: {
    roleId: {
      // 当前用户的id
      type: String,
      required: true
    }
  },
  data() {
    return {
      roleIds: [], // 这个用于展示双向绑定选中的角色
      list: [] // 这个用于保存 获取到的角色项
    }
  },
  created() {
    this.loadGetRoles()
  },
  methods: {
    // 发送修改好的权限数据
    async hSubmit() {
      try {
        const res = await assignRoles({ id: this.roleId, roleIds: this.roleIds })
        console.log(res)
        this.loadGetRoles()
        this.$emit('close')
      } catch (error) {
        console.log(error)
      }
    },
    // 获取部门分配角色
    async loadGetRoles() {
      try {
        const res = await getRoles()
        console.log('getRoles', res)
        // 保存数据
        this.list = res.data.rows
        const info = await getUserProfileId(this.roleId)
        console.log('info 这是获取员工身上有几个角色的', info)
        console.log(info.data.roleIds)
        this.roleIds = info.data.roleIds
      } catch (err) {
        console.log('getRoles', err)
      }
    },
    closeDialog() {
      this.$emit('close')
      console.log(11111)
    }
  }
}
</script>
